using PKGames.Voxel.Analytics;
using System.Collections.Generic;
using System.Linq;
using VoxelEngine;
using VoxelGame.Analytics;
using VoxelGame.Character;

namespace PKGames.Voxel
{
	public class AnalyticsController
	{
		private readonly VoxelGameSessionEvent _voxelGameSessionEvent;

		private readonly BlockInteractionEventGroup _blockInteractionEventGroup;

		private readonly NpcInteractionEventGroup _npcInteractionEventGroup;

		private readonly ToolInteractionEventGroup _toolInteractionEventGroup;

		public AnalyticsController(string levelName, bool isTrial)
		{
			_voxelGameSessionEvent = new VoxelGameSessionEvent(levelName, isTrial);
			_blockInteractionEventGroup = new BlockInteractionEventGroup(levelName);
			_npcInteractionEventGroup = new NpcInteractionEventGroup(levelName);
			_toolInteractionEventGroup = new ToolInteractionEventGroup();
		}

		public void SetLoadingTime(int loadingTime)
		{
			_voxelGameSessionEvent.SetLoadingTime(loadingTime);
		}

		public void StartFlyTimer()
		{
			_voxelGameSessionEvent.StartFlyTimer();
		}

		public void StopFlyTimer()
		{
			_voxelGameSessionEvent.StopFlyTimer();
		}

		public void BlockAdded(string blockType, CharacterToolsController.ToolData.Tools currentTool)
		{
			_voxelGameSessionEvent.BlockAdded();
			_blockInteractionEventGroup.BlockAdded(blockType, 1);
			_toolInteractionEventGroup.BlockAdded(currentTool);
		}

		public void BlockRemoved(string blockType, CharacterToolsController.ToolData.Tools currentTool)
		{
			_voxelGameSessionEvent.IncrementBlocksRemoved(1);
			_blockInteractionEventGroup.BlockRemoved(blockType, 1);
			_toolInteractionEventGroup.BlockRemoved(currentTool, 1);
		}

		public void MultipleBlocksRemoved(Dictionary<BlockType, int> removeCount, CharacterToolsController.ToolData.Tools currentTool)
		{
			int num = removeCount.Values.Sum();
			_voxelGameSessionEvent.IncrementBlocksRemoved(num);
			_toolInteractionEventGroup.BlockRemoved(currentTool, num);
			foreach (KeyValuePair<BlockType, int> item in removeCount)
			{
				_blockInteractionEventGroup.BlockRemoved(item.Key.ToString(), item.Value);
			}
		}

		public void NpcInteractionMade(string npc)
		{
			_voxelGameSessionEvent.OnNpcInteraction();
			_npcInteractionEventGroup.OnNpcInteraction(npc);
		}

		public void ToolSelected(CharacterToolsController.ToolData.Tools toolSelected)
		{
			_toolInteractionEventGroup.OnToolUse(toolSelected);
		}

		public void SendAnalyticsEvents(string worldId, float farClipMode)
		{
			_voxelGameSessionEvent.SetFarClipPlane(farClipMode);
			_voxelGameSessionEvent.SetWorldId(worldId);
			_voxelGameSessionEvent.Finish();
			_voxelGameSessionEvent.Log();
			_blockInteractionEventGroup.FinishAndLog();
			_npcInteractionEventGroup.FinishAndLog();
			_toolInteractionEventGroup.FinishAndLog();
		}
	}
}
